//
//  HonorManagementViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/8/6.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "HonorManagementViewController.h"
#import "TongXunLuViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface HonorManagementViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UITextViewDelegate>{
    
    UILabel *nameLabel,*rankLabel;
    UIButton *nameBtn,*rankBtn;
    UITextView *contentView;
    UIPickerView *rankPickerView;
    NSArray *rankArray;
}

@end

@implementation HonorManagementViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title=@"村务公开";
    self.view.backgroundColor=[UIColor whiteColor];
    self.honorUserid=@"";
    rankArray=[[NSArray alloc]initWithObjects:@"不排名",@"1",@"2",@"3", nil];
    [self createNavButton];
    [self createView];
}

-(void)createView{
    
    float height;
    if (SCREEN_WIDTH==320&&SCREEN_HEIGHT==480) {
        height=0;
    }else{
        height=10;
    }
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 40, 30)];
    titleLabel.text=@"姓名";
    titleLabel.textAlignment=0;
    [self.view addSubview:titleLabel];
    
    UILabel *ranLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 45+height, 40, 30)];
    ranLabel.text=@"排名";
    ranLabel.textAlignment=0;
    [self.view addSubview:ranLabel];
    
    UILabel *contentLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 80+height*2, 40, 30)];
    contentLabel.text=@"贡献";
    contentLabel.textAlignment=0;
    [self.view addSubview:contentLabel];
    
    nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, SCREEN_WIDTH-80, 30)];
    nameLabel.textAlignment=0;
    nameLabel.layer.borderWidth =0.5;
    nameLabel.layer.cornerRadius =5.0;
    [self.view addSubview:nameLabel];
    
    nameBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    nameBtn.frame=CGRectMake(60, 10, SCREEN_WIDTH-80, 30);
    [nameBtn addTarget:self action:@selector(chooseName) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nameBtn];
    
    rankLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 45+height, SCREEN_WIDTH-80, 30)];
    rankLabel.textAlignment=0;
    rankLabel.text=@"不排名";
    rankLabel.layer.borderWidth =0.5;
    rankLabel.layer.cornerRadius =5.0;
    [self.view addSubview:rankLabel];
    
    rankBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rankBtn.frame=CGRectMake(60, 45+height, SCREEN_WIDTH-80, 30);
    [rankBtn addTarget:self action:@selector(chooseRank) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rankBtn];
    
    contentView=[[UITextView alloc]initWithFrame:CGRectMake(60, 80+height*2, SCREEN_WIDTH-80, 120)];
    contentView.layer.borderColor = [UIColor grayColor].CGColor;
    contentView.font=[UIFont systemFontOfSize:17];
    contentView.delegate=self;
    contentView.layer.borderWidth =1.0;
    contentView.layer.cornerRadius =5.0;
    [self.view addSubview:contentView];
    
    UILabel *tiShiLabel=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-122, 200+height*2, 100, 30)];
    tiShiLabel.text=@"限30字以内";
    tiShiLabel.textAlignment=2;
    [self.view addSubview:tiShiLabel];
}

-(void)chooseName{
    
    TongXunLuViewController *tongVC=[[TongXunLuViewController alloc]init];
    tongVC.orHonorName=YES;
    tongVC.orRefresh=YES;
    [self.navigationController pushViewController:tongVC animated:YES];
}

-(void)chooseRank{
    
    [rankPickerView removeFromSuperview];
    rankPickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(rankBtn.frame.origin.x, rankBtn.frame.origin.y+30, rankBtn.frame.size.width, 30)];
    rankPickerView.dataSource=self;
    rankPickerView.delegate=self;
    rankPickerView.backgroundColor=[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    [self.view addSubview:rankPickerView];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;  //这个picker里的组键数
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [rankArray count];
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return [rankArray objectAtIndex:row];
}

-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component{
    
    rankLabel.text=[rankArray objectAtIndex:row];
    [self performSelector:@selector(removePickerView) withObject:nil afterDelay:0.5f];
}

-(void)removePickerView{
    
    [rankPickerView removeFromSuperview];
}

-(void)createNavButton{
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
    [rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(publishHonor) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)publishHonor{
    
    if ((nameLabel.text.length<1)||[contentView.text isEqualToString:@""]) {
        
        [self showAlertWithMessage:@"姓名或贡献不能为空!"];
        return;
        
    }else{
        
        NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
        [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
        [CheckParams setObject:[Config getOwnAreaids] forKey:@"areaids"];
        [CheckParams setObject:self.honorUserid forKey:@"honoruserid"];
        [CheckParams setObject:contentView.text forKey:@"content"];
        
        if ([rankLabel.text isEqualToString:@"不排名"]) {
            [CheckParams setObject:@"9999" forKey:@"honorlevel"];
        }else{
            [CheckParams setObject:rankLabel.text forKey:@"honorlevel"];
        }
        NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
        [CheckParams setObject:resultSign forKey:@"sign"];
        [MyDataService requestURL:@"op_honorpublish.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
            
            NSLog(@"%@",result);
            
            if ([[result objectForKey:@"response"] isEqualToString:@"op_honorpublish"]) {
                
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
                hud.labelText = @"光荣榜发布成功!";
                [hud hide:YES afterDelay:1];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                
                MBProgressHUD *hud = [Config createHUD];
                hud.mode = MBProgressHUDModeCustomView;
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
                hud.labelText = @"光荣榜发布失败!";
                [hud hide:YES afterDelay:1];
            }
            
        }failureAction:^{
            
        }];
    }
}

//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length>30) {
        
        [self contentViewBeyond];
    }
}

-(void)contentViewBeyond{
    
    if (contentView.text.length>30) {
        
        [self showAlertWithMessage:@"限30字以内!"];
        contentView.text=[contentView.text substringWithRange:NSMakeRange(0, 29)];
    }
    
}

- (void)showAlertWithMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    nameLabel.text=self.nameString;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    [rankPickerView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
